
package com.zjff.myshop.front.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjff.myshop.front.pojo.vo.GoodsVO;
import com.zjff.myshop.front.pojo.vo.StockCountVO;
import com.zjff.myshop.entity.Goods;

import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.mapper.GoodsMapper;
import com.zjff.myshop.front.service.GoodsService;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.PageList;
import com.zjff.myshop.util.PageQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    // 新增商品
    @Override
    public Boolean insert(Goods goods) {
        goods.setId(null);
        return goodsMapper.insert(goods) > 0;
    }

    // 删除单个商品
    @Override
    public Boolean deleteById(Integer id) {
        //检查记录是否存在
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        if (goods == null) {
            throw new BusinessException("id对应的商品不存在");
        }
        return goodsMapper.deleteByPrimaryKey(id) > 0;
    }

    // 删除多个商品
    @Override
    public Boolean deleteBatch(Integer[] ids) {
        return goodsMapper.deleteBatch(ids) > 0;
    }

    //更改商品
    @Override
    public Boolean update(Goods goods) {
        //检查参数
        Integer goodsId = goods.getId();
        if (goodsId == null || goodsId <= 0) {
            throw new BusinessException("参数错误");
        }
        //更新
        return goodsMapper.update(goods) > 0;
    }

    //更改商品库存
    @Override
    public Boolean updateByStockCountList(List<StockCountVO> stockCountList) {
        return goodsMapper.updateByStockCountList(stockCountList) > 0;
    }

    //查询商品id对应的商品详情
    @Override
    public Goods selectByPrimaryKey(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Goods> selectByIds(Integer[] ids) {
        if (ids.length == 0) {
            return new ArrayList<>();
        }
        return goodsMapper.selectByIds(ids);
    }

    //分页查询（筛选商品分类、名称关键字、状态、排序）
    @Override
    public PageList<Goods> selectPage(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getLimit());
        List<Goods> goodsList = goodsMapper.selectPage(pageQuery);
        int total = (int) new PageInfo<>(goodsList).getTotal();
        return new PageList<>(goodsList, total, pageQuery.getLimit(), pageQuery.getPage());
    }

    public PageList<GoodsVO> selectVoPage(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getLimit());
        List<Goods> goodsList = goodsMapper.selectPage(pageQuery);
        int total = (int) new PageInfo<>(goodsList).getTotal();

        List<GoodsVO> goodsVOList = BeanUtils.copyList(goodsList, GoodsVO.class);
        return new PageList<>(goodsVOList, total, pageQuery.getLimit(), pageQuery.getPage());
    }

}
